package frame.system.mapper;

import frame.system.to.Dept;
import frame.system.qo.AuthQo;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * 对应表：sys_dept 对于实体类：Dept
 * <br>
 * @author PeiWentao
 * @since 2020-06-03 16:35
 */
@Repository
public interface DeptMapper extends Mapper<Dept> {

	// 字段
    String FIELDS = "t.pid,t.type,t.name,t.position,t.manager_phone,t.description,t.valid,t.create_time,t.update_time,t.id";
    // 表名 as 别名
    String TABLE = "sys_dept t";
    // mapper包名
    String PACKAGE = "frame.system.mapper";

    @Results({
            @Result(column = "id", property = "id",id = true),
            @Result(column = "id", property = "userCount", one = @One(select = PACKAGE + ".UserMapper.countByDeptId")),
            @Result(column = "manager_phone", property = "managerPhone"),
            @Result(column = "manager_phone", property = "manager", one = @One(select = PACKAGE + ".UserMapper.cascadeByUsername"))
    })
    @Select({"<script>SELECT ", FIELDS," FROM ", TABLE,
            " WHERE t.valid=#{valid}",
            "<when test='type!=null'> AND t.type=#{type}</when>",
            "<when test='name!=null'> AND name LIKE CONCAT('%',#{name},'%')</when>",
            "ORDER BY ${orderStr}",
            "</script>"})
    List<Dept> pageList(AuthQo qo);

}
